package com.itheima.ums.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.api.config.filter.IFilterConfig;
import com.itheima.mall.pojo.Admin;
import com.itheima.mall.pojo.Role;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.UserRightsVo;
import com.itheima.ums.service.UmsAdminService;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Resource
    private UmsAdminService umsAdminService;

    /**
     * 注册用户
     *
     * @param admin
     * @return
     */
    @PostMapping("/register")
    public ResponseEntity register(@RequestBody Admin admin) {
        Admin adminInfo = umsAdminService.register(admin);
        if (adminInfo == null) {
            return ResponseEntity.ok("ok");
        }

        return ResponseEntity.ok(adminInfo);
    }

    /**
     * 用户登录
     *
     * @param admin
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Admin admin) {
        Map map = umsAdminService.login(admin);
        if (CollUtil.isEmpty(map)) {
            return ResponseEntity.ok("OK");
        }
//        ResponseEntity.status(200).body(map);
        return ResponseEntity.status(200).body(map);
    }

    /**
     * 刷新token
     *
     * @return
     */
    @GetMapping("/refreshToken")
    public ResponseEntity refreshToken() {
        Map map = umsAdminService.refreshToken();
        if (CollUtil.isEmpty(map)) {
            return ResponseEntity.ok("OK");
        }
        return ResponseEntity.ok(map);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return
     */
    @GetMapping("/info")
    public ResponseEntity info() {
        UserRightsVo vo = umsAdminService.info();
        if (vo == null) {
            return ResponseEntity.ok("ok");
        }

        return ResponseEntity.ok(vo);
    }

    /**
     * 根据用户名或姓名分页获取用户列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity selectAdminPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          String keyword) {
        PageResult pageResult = umsAdminService.list(pageNum, pageSize, keyword);
        if (pageResult == null) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 登出功能
     *
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity logout() {
        umsAdminService.logout();
        return ResponseEntity.ok("true");
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseEntity deleteById(@PathVariable("id") Long id) {
        Integer count = umsAdminService.deleteById(id);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 给用户分配角色
     *
     * @param
     * @return
     */
    @PostMapping("/role/update")
    public ResponseEntity updateRole(Long adminId, @RequestParam List<Long> roleIds) {
        Integer count = umsAdminService.updateRole(adminId, roleIds);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }

    /**
     * 获取指定用户的角色
     *
     * @param adminId
     * @return
     */
    @GetMapping("/role/{adminId}")
    public ResponseEntity getRoleInfoById(@PathVariable("adminId") Long adminId) {
        List<Role> list = umsAdminService.getRoleInfoById(adminId);
        if (CollUtil.isEmpty(list)) {
            return ResponseEntity.ok("ok");
        }

        return ResponseEntity.ok(list);
    }

    /**
     * 根据id修改用户信息
     *
     * @param admin
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateById(@RequestBody Admin admin, @PathVariable("id") Long id) {
        Integer count = umsAdminService.updateById(admin, id);
        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);
    }


    /**
     * 修改帐号状态
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{id}")
    public ResponseEntity updateStatus(@PathVariable("id") Long id,
                                       @RequestParam(name = "status") Long status) {
        Integer count = umsAdminService.updateStatus(id, status);

        if (count <= 0) {
            return ResponseEntity.ok("ok");
        }
        return ResponseEntity.ok(count);

    }

    /**
     * 获取指定用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity getAdminById(@PathVariable("id") Long id) {
        Admin admin = umsAdminService.getAdminById(id);
        if (admin == null) {
            return ResponseEntity.ok("ok");
        }

        return ResponseEntity.ok(admin);
    }


}
